package org.springframework.prospring.ticket.dao.hibernate;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.prospring.ticket.dao.SportDAO;
import org.springframework.prospring.ticket.domain.location.Region;
import org.springframework.prospring.ticket.domain.sport.Sport;
import org.springframework.prospring.ticket.exception.BusinessException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.lang.StringUtils;

import java.util.Collection;

/**
 * This class interacts with Spring and Hibernate to save and
 * retrieve User objects.
 *
 * @author Alexis Quiros
 */
public class SportDAOHibernate extends HibernateDaoSupport implements SportDAO {
    private static Log logger = LogFactory.getLog(SportDAOHibernate.class);

    public SportDAOHibernate() {
        super();
        if (logger.isDebugEnabled()) {
            logger.debug("in SportDAOHibernate Constructor");
        }
    }

    public Collection<Sport> findAllSports(){
        if (logger.isDebugEnabled()) {
            logger.debug("finding all Sports ");
        }

        Collection sports = getHibernateTemplate().find("from Sport");

        return sports;
    }
    public void saveSport(Sport sport) throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("Saving Sport: " + sport);
        }

        if (StringUtils.isEmpty(sport.getName())) {
            String message = "The name is required, name="+sport.getName();
            logger.info(message);
            throw new BusinessException(message);
        }

        if (StringUtils.isEmpty(sport.getBusinessPK())) {
            String message = "The businessPK is required, businessPK="+sport.getBusinessPK();
            logger.info(message);
            throw new BusinessException(message);
        }

        getHibernateTemplate().saveOrUpdate(sport);

        if (logger.isDebugEnabled()) {
            logger.debug("sportId set to: " + sport.getId());
        }
    }

    public void removeSport(Long sportId) {
        if (logger.isDebugEnabled()) {
            logger.debug("removing Sport id: " + sportId);
        }

        Object sport = getHibernateTemplate().load(Sport.class, sportId);
        getHibernateTemplate().delete(sport);
    }

    public Sport findByBusinessPK(String sportBusinessPK) {
        if (logger.isDebugEnabled()) {
            logger.debug("finding Sport by businessPK="+sportBusinessPK);
        }

        Sport sport = null;
        String queryString = "from Sport s where (s.businessPK = :businessPK)";
        Sport sportParameter = new Sport();
        sportParameter.setBusinessPK(sportBusinessPK);
        Collection<Sport> sports = getHibernateTemplate().findByValueBean(queryString, sportParameter);
        //Collection<Region> sports = getHibernateTemplate().findByValueBean.find("from Region r where r.businessPK="+sportBusinessPK);

        if ((sports != null) && (!sports.isEmpty())){
            sport = sports.iterator().next();
        }

        return sport;
    }


}
